//
//  XiaofeiDetailViewController.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/20.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "XiaofeiDetailViewController.h"

#import "XiaofeiDayDetailViewController.h"

@interface XiaofeiDetailViewController () <UITableViewDelegate,UITableViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) UITableView * tableView;

//头视图
@property (nonatomic, strong) UIView * view_head;

//下拉菜单
@property (nonatomic, strong) UIView * view_pull;
@property (nonatomic, copy) NSString * str_pullType;

@property (nonatomic, strong) UIButton * btn_day;
@property (nonatomic, strong) UIButton * btn_month;
@property (nonatomic, strong) UIButton * btn_year;


//数据
@property (nonatomic, assign) NSInteger number;


@end

@implementation XiaofeiDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //状态
    self.str_pullType = @"0";
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    //11个字
    _lblTitle.text = [NSString stringWithFormat:@"山东智顺网络有限公司"];
    _lblTitle.font = [UIFont systemFontOfSize:20];
    _lblTitle.numberOfLines = 1;
    
    [self addRightButton:@"rli@2x.png"];
    _imgRight.frame = CGRectMake(SCREEN_WIDTH - 5 - 30, _imgRight.frame.origin.y + 2, _imgRight.frame.size.width - 5, _imgRight.frame.size.height - 5);
    _btnRight.frame = CGRectMake(SCREEN_WIDTH - 10 - 40, _lblRight.frame.origin.y + 0, 40, _lblRight.frame.size.height - 10);
    
    [self addLeftButton:@"fanhui@2x.png"];
}

//左返回
- (void)clickLeftButton:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clickRightButton:(UIButton *)sender
{
    if(self.view_pull.frame.size.height == 0)
    {
        self.view_pull = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 80, 64, 80, 0)];
        self.view_pull.backgroundColor = [UIColor whiteColor];
        //1
        self.btn_day = [UIButton buttonWithType:(UIButtonTypeSystem)];
        self.btn_day.frame = CGRectMake(0, 0, 80, 40);
        [self.btn_day setTitle:@"日扣费" forState:(UIControlStateNormal)];
        self.btn_day.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.btn_day setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [self.view_pull addSubview:self.btn_day];
        [self.btn_day addTarget:self action:@selector(btn_dayAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //2
        self.btn_month = [UIButton buttonWithType:(UIButtonTypeSystem)];
        self.btn_month.frame = CGRectMake(0, 40, 80, 40);
        [self.btn_month setTitle:@"月扣费" forState:(UIControlStateNormal)];
        self.btn_month.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.btn_month setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [self.view_pull addSubview:self.btn_month];
        [self.btn_month addTarget:self action:@selector(btn_monthAction:) forControlEvents:(UIControlEventTouchUpInside)];

        //3
        self.btn_year = [UIButton buttonWithType:(UIButtonTypeSystem)];
        self.btn_year.frame = CGRectMake(0, 80, 80, 40);
        [self.btn_year setTitle:@"年扣费" forState:(UIControlStateNormal)];
        self.btn_year.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.btn_year setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [self.view_pull addSubview:self.btn_year];
        [self.btn_year addTarget:self action:@selector(btn_yearAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //保留痕迹
        switch ([self.str_pullType integerValue])
        {
            case 0:
            {
                [self.btn_day setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
            }
                break;
            case 1:
            {
                [self.btn_month setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
            }
                break;
            case 2:
            {
                [self.btn_year setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
            }
                break;
            default:
                break;
        }
        
        [UIView animateWithDuration:0.1 animations:^{
            
            self.view_pull.frame = CGRectMake(SCREEN_WIDTH - 80, 64, 80, 120);
            
        } completion:^(BOOL finished) {
            
        }];
        
        [self.view addSubview:self.view_pull];
//        [self.view bringSubviewToFront:self.view_pull];
    }
    else
    {
        [UIView animateWithDuration:0.1 animations:^{
            
            self.view_pull.frame = CGRectMake(SCREEN_WIDTH - 80, 64, 80, 0);
            
        } completion:^(BOOL finished) {
            
            self.view_pull.hidden = YES;
            
        }];

    }
}

//隐藏tabbar
-(void)viewWillAppear:(BOOL)animated
{
    //处于隐藏状态
    self.view_pull.hidden = YES;
    
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void)p_setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    

    //
    __unsafe_unretained UITableView *tableView = self.tableView;
    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 下拉刷新
    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf p_data];
        
        // 结束刷新
        [tableView.mj_header endRefreshing];
    }];
    
    // 马上进入刷新状态
    [tableView.mj_header beginRefreshing];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    tableView.mj_header.automaticallyChangeAlpha = YES;
    
//    // 上拉加载
//    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//
//        [weakSelf p_data1];
//        // 结束刷新
//        [tableView.mj_footer endRefreshing];
//    }];
}

#pragma mark - 数据
- (void )p_data
{
    self.number = 30;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.number;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    cell.backgroundColor = [UIColor whiteColor];
    
    switch ([self.str_pullType integerValue])
    {
        case 0:
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            CGFloat list_x = (SCREEN_WIDTH - 10) / 4;
            
            //需要空2格
            UILabel * lbl_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 , 7.5, list_x * 1.5, 30)];
            lbl_1.text = [NSString stringWithFormat:@"  12-%d %d:21:00",indexPath.row + 1,indexPath.row];
            lbl_1.textColor = [UIColor blackColor];
            lbl_1.font = [UIFont systemFontOfSize:15];
            [cell addSubview:lbl_1];
            
            UILabel * lbl_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 1.5, 7.5, list_x, 30)];
            lbl_2.text = [NSString stringWithFormat:@"%d",indexPath.row + 11];
            lbl_2.textColor = [UIColor blackColor];
            lbl_2.textAlignment = NSTextAlignmentCenter;
            lbl_2.font = [UIFont systemFontOfSize:15];
            [cell addSubview:lbl_2];
            
            UILabel * lbl_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 2.5, 7.5, list_x * 1.5, 30)];
            lbl_3.text = [NSString stringWithFormat:@"￥ %d2.70",indexPath.row];
            lbl_3.textColor = [UIColor blackColor];
            lbl_3.textAlignment = NSTextAlignmentCenter;
            lbl_3.font = [UIFont systemFontOfSize:15];
            [cell addSubview:lbl_3];
        }
            break;
        case 1:
        {
            CGFloat list_x = (SCREEN_WIDTH - 30) / 4;

            //需要空3格
            UILabel * lbl_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 , 7.5, list_x, 30)];
            lbl_1.text = [NSString stringWithFormat:@"   2016-%d",indexPath.row + 1];
            lbl_1.textColor = [UIColor blackColor];
            lbl_1.font = [UIFont systemFontOfSize:15];
            [cell addSubview:lbl_1];
            
            UILabel * lbl_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x, 7.5, list_x * 1.5, 30)];
            lbl_2.text = [NSString stringWithFormat:@"%d",31 - indexPath.row];
            lbl_2.textColor = [UIColor blackColor];
            lbl_2.textAlignment = NSTextAlignmentCenter;
            lbl_2.font = [UIFont systemFontOfSize:15];
            [cell addSubview:lbl_2];
            
            UILabel * lbl_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 2.5, 7.5, list_x * 1.5, 30)];
            lbl_3.text = [NSString stringWithFormat:@"￥ 2%d.70",indexPath.row];
            lbl_3.textColor = [UIColor blackColor];
            lbl_3.textAlignment = NSTextAlignmentCenter;
            lbl_3.font = [UIFont systemFontOfSize:15];
            [cell addSubview:lbl_3];
            
            UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(5 + list_x * 4 + 2.5, 12.5, 20, 20)];
            image.image = [UIImage imageNamed:@"youfanhui.png"];
            [cell addSubview:image];
        }
            break;
        case 2:
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            CGFloat list_x = (SCREEN_WIDTH - 10) / 3;
            //需要空3格
            UILabel * lbl_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 , 7.5, list_x, 30)];
            lbl_1.text = [NSString stringWithFormat:@"   20%d年度",indexPath.row + 10];
            lbl_1.textColor = [UIColor blackColor];
            lbl_1.font = [UIFont systemFontOfSize:15];
            [cell addSubview:lbl_1];
            
            UILabel * lbl_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x, 7.5, list_x, 30)];
            lbl_2.text = [NSString stringWithFormat:@"%d",287 - indexPath.row * 2];
            lbl_2.textColor = [UIColor blackColor];
            lbl_2.textAlignment = NSTextAlignmentCenter;
            lbl_2.font = [UIFont systemFontOfSize:15];
            [cell addSubview:lbl_2];
            
            UILabel * lbl_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 2, 7.5, list_x, 30)];
            lbl_3.text = [NSString stringWithFormat:@"￥ 1%d.70",indexPath.row];
            lbl_3.textColor = [UIColor blackColor];
            lbl_3.textAlignment = NSTextAlignmentCenter;
            lbl_3.font = [UIFont systemFontOfSize:15];
            [cell addSubview:lbl_3];
        }
            break;
            
        default:
            break;
    }
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.view_head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
    self.view_head.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    switch ([self.str_pullType integerValue])
    {
        case 0:
        {
            CGFloat list_x = (SCREEN_WIDTH - 10) / 4;
            
            //1
            UILabel * list_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 , 7.5, list_x * 1.5, 20)];
            list_1.text = @"  日期";
            list_1.textColor = navi_bar_bg_color;
            list_1.font = [UIFont systemFontOfSize:13];
            //    list_1.textAlignment = NSTextAlignmentCenter;
            [self.view_head addSubview:list_1];
            
            //2
            UILabel * list_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 1.5, 7.5, list_x * 1, 20)];
            list_2.text = @"排名";
            list_2.textColor = navi_bar_bg_color;
            list_2.font = [UIFont systemFontOfSize:13];
            list_2.textAlignment = NSTextAlignmentCenter;
            [self.view_head addSubview:list_2];
            
            //3
            UILabel * list_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 2.5, 7.5, list_x * 1.5, 20)];
            list_3.text = @"扣费";
            list_3.textColor = navi_bar_bg_color;
            list_3.font = [UIFont systemFontOfSize:13];
            list_3.textAlignment = NSTextAlignmentCenter;
            [self.view_head addSubview:list_3];
        }
            break;
        case 1:
        {
            CGFloat list_x = (SCREEN_WIDTH - 30) / 4;
            
            //1
            UILabel * list_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 , 7.5, list_x * 1, 20)];
            list_1.text = @"   月份";
            list_1.textColor = navi_bar_bg_color;
            list_1.font = [UIFont systemFontOfSize:13];
            //    list_1.textAlignment = NSTextAlignmentCenter;
            [self.view_head addSubview:list_1];
            
            //2
            UILabel * list_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 1, 7.5, list_x * 1.5, 20)];
            list_2.text = @"在线天数";
            list_2.textColor = navi_bar_bg_color;
            list_2.font = [UIFont systemFontOfSize:13];
            list_2.textAlignment = NSTextAlignmentCenter;
            [self.view_head addSubview:list_2];
            
            //3
            UILabel * list_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 2.5, 7.5, list_x * 1.5, 20)];
            list_3.text = @"该月总扣费";
            list_3.textColor = navi_bar_bg_color;
            list_3.font = [UIFont systemFontOfSize:13];
            list_3.textAlignment = NSTextAlignmentCenter;
            [self.view_head addSubview:list_3];
        }
            break;
        case 2:
        {
            CGFloat list_x = (SCREEN_WIDTH - 10) / 3;
            
            //1
            UILabel * list_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 , 7.5, list_x * 1, 20)];
            list_1.text = @"   年份";
            list_1.textColor = navi_bar_bg_color;
            list_1.font = [UIFont systemFontOfSize:13];
            //    list_1.textAlignment = NSTextAlignmentCenter;
            [self.view_head addSubview:list_1];
            
            //2
            UILabel * list_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 1, 7.5, list_x * 1, 20)];
            list_2.text = @"在线天数";
            list_2.textColor = navi_bar_bg_color;
            list_2.font = [UIFont systemFontOfSize:13];
            list_2.textAlignment = NSTextAlignmentCenter;
            [self.view_head addSubview:list_2];
            
            //3
            UILabel * list_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 2, 7.5, list_x * 1, 20)];
            list_3.text = @"该年总扣费";
            list_3.textColor = navi_bar_bg_color;
            list_3.font = [UIFont systemFontOfSize:13];
            list_3.textAlignment = NSTextAlignmentCenter;
            [self.view_head addSubview:list_3];
            
        }
            break;
        default:
            break;
    }
    
    return self.view_head;
}

- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if([self.str_pullType isEqualToString:@"1"])
    {
        XiaofeiDayDetailViewController * xiaofeiDayDetailViewController = [[XiaofeiDayDetailViewController alloc] init];
        
        [self showViewController:xiaofeiDayDetailViewController sender:nil];
    }
}

#pragma mark - scrollView代理
- (void )scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [UIView animateWithDuration:0.1 animations:^{
        
        self.view_pull.frame = CGRectMake(SCREEN_WIDTH - 80, 64, 80, 0);
        
    } completion:^(BOOL finished) {
        
        self.view_pull.hidden = YES;
        
    }];
}

- (void )touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:0.1 animations:^{
        
        self.view_pull.frame = CGRectMake(SCREEN_WIDTH - 80, 64, 80, 0);
        
    } completion:^(BOOL finished) {
        
        self.view_pull.hidden = YES;
        
    }];
}


#pragma mark - 右侧点击事件
//日
- (void)btn_dayAction:(UIButton *)sender
{
    self.str_pullType = @"0";
    
    [UIView animateWithDuration:0.1 animations:^{
        
        self.view_pull.frame = CGRectMake(SCREEN_WIDTH - 80, 64, 80, 0);
        
    } completion:^(BOOL finished) {
        
        self.view_pull.hidden = YES;
        
    }];
    
    [self.btn_day setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
    
    
    [self.btn_month setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_year setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
}

//月
- (void)btn_monthAction:(UIButton *)sender
{
    self.str_pullType = @"1";
    
    [UIView animateWithDuration:0.1 animations:^{
        
        self.view_pull.frame = CGRectMake(SCREEN_WIDTH - 80, 64, 80, 0);
        
    } completion:^(BOOL finished) {
        
        self.view_pull.hidden = YES;
        
    }];
    
    [self.btn_month setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
    
    [self.btn_day setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_year setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
}

//年
- (void)btn_yearAction:(UIButton *)sender
{
    self.str_pullType = @"2";
    
    [UIView animateWithDuration:0.1 animations:^{
        
        self.view_pull.frame = CGRectMake(SCREEN_WIDTH - 80, 64, 80, 0);
        
    } completion:^(BOOL finished) {
        
        self.view_pull.hidden = YES;
        
    }];
    
    [self.btn_year setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
    
    [self.btn_day setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.btn_month setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
}







@end
